package mrms.api.web.controllers;

import lombok.AllArgsConstructor;
import mrms.api.entity.MeetingEntity;
import mrms.api.entity.NoticeEntity;
import mrms.api.entity.UserEntity;
import mrms.api.repository.MeetingRepository;
import mrms.api.repository.NoticeRepository;
import mrms.api.repository.UserRepository;
import mrms.api.service.MeetingService;
import mrms.api.service.MessageService;
import mrms.api.web.dto.NoticeDto;
import mrms.api.web.utils.MessageType;
import mrms.api.web.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author lenchu
 * @date 2019/02/06
 */
@RestController
@AllArgsConstructor
public class NoticeController {
    private final NoticeRepository noticeRepository;
    private final MeetingRepository meetingRepository;
    private final MessageService messageService;
    private final MeetingService meetingService;
    private final UserRepository userRepository;

    @PostMapping("/notice")
    public Result save(@RequestBody NoticeDto noticeDto, @RequestAttribute("user")UserEntity user) {
        Optional<MeetingEntity> optionalMeetingEntity = meetingRepository.findById(noticeDto.getMeetingId());
        if (optionalMeetingEntity.isPresent()) {
            MeetingEntity meetingEntity = optionalMeetingEntity.get();
            if (meetingEntity.getSponsor().equals(user.getId())) {
                NoticeEntity noticeEntity = new NoticeEntity();
                BeanUtils.copyProperties(noticeDto, noticeEntity);
                noticeEntity.setCreatetime(System.currentTimeMillis());
                noticeEntity.setMeeting(noticeDto.getMeetingId());

                noticeEntity = noticeRepository.save(noticeEntity);

                List<UserEntity> members = meetingService.getAllMembersByMeetingId(meetingEntity.getId());
                String[] users = members.stream().map(userEntity -> userEntity.getId()).collect(Collectors.toList()).toArray(new String[members.size()]);

                Map<String, Object> map = new HashMap<>(2);
                meetingEntity.setSponsorDetail(userRepository.findById(meetingEntity.getSponsor()).get());
                map.put("meeting", meetingEntity);
                map.put("notice", noticeEntity);
                messageService.newMessage(MessageType.NOTICE, map, users);
                return Result.success(noticeEntity);
            } else {
                return Result.error("您不是会议发起人");
            }
        } else {
            return Result.error("无此会议");
        }
    }

    @DeleteMapping("/notice/{id}")
    public Result delete(@PathVariable Integer id) {
        Optional<NoticeEntity> optionalNoticeEntity = noticeRepository.findById(id);
        if (optionalNoticeEntity.isPresent()) {
            NoticeEntity noticeEntity = optionalNoticeEntity.get();
            noticeRepository.deleteById(noticeEntity.getId());
            return Result.success(noticeEntity);
        } else {
            return Result.error("无此公告");
        }
    }

    @GetMapping("/notice/m/{id}")
    public Result findAllByMeeting(@PathVariable Integer id) {
        Optional<MeetingEntity> optionalMeetingEntity = meetingRepository.findById(id);
        if (optionalMeetingEntity.isPresent()) {
            MeetingEntity meetingEntity = optionalMeetingEntity.get();
            List<NoticeEntity> noticeEntities = noticeRepository.findAllByMeeting(meetingEntity.getId());
            return Result.success(noticeEntities);
        } else {
            return Result.error("无此会议");
        }
    }

    @GetMapping("/notice/{id}")
    public Result findById(@PathVariable Integer id) {
        Optional<NoticeEntity> optionalNoticeEntity = noticeRepository.findById(id);
        if (optionalNoticeEntity.isPresent()) {
            NoticeEntity noticeEntity = optionalNoticeEntity.get();
            return Result.success(noticeEntity);
        } else {
            return Result.error("无此公告");
        }
    }
}
